package com.bupt.utils;

import com.bupt.basic.IBasicNetWork;

import java.util.List;

/**
 * 确定两个链路相关的工具类
 * 如果相关返回true
 * 无关返回false
 */
public class TopoRelevanceUtils {
    public static boolean check(List<IBasicNetWork.BasicLink> links, IBasicNetWork.BasicLink link){
        if(links == null || link == null){
            return false;
        }
        for(IBasicNetWork.BasicLink link1: links){
            if(check(link1, link)){
                return true;
            }
        }
        return false;
    }
    public static boolean check(List<IBasicNetWork.BasicLink> links, IBasicNetWork.BasicNode node){
        if(links == null || node == null){
            return false;
        }
        for(IBasicNetWork.BasicLink link1: links){
            if(check(link1, node)){
                return true;
            }
        }
        return false;
    }
    public static boolean check(IBasicNetWork.BasicLink val1, IBasicNetWork.BasicLink val2){
        if(val1 == null || val2 == null){
            return false;
        }
        if(val1.equals(val2)){
            return true;
        }
        if(val1 instanceof IBasicNetWork.FiberLink){
            if(val2 instanceof IBasicNetWork.FiberLink){

                if(val1.equals(val2) ||
                        val1.getSourceNode().equals(val2.getSourceNode()) ||
                        val1.getSourceNode().equals(val2.getTargetNode()) ||
                        val1.getTargetNode().equals(val2.getTargetNode()) ||
                        val1.getTargetNode().equals(val2.getSourceNode())
                ){
                    return true;
                }
            }else {
                if(val2 instanceof IBasicNetWork.WdmLink){
                    IBasicNetWork.WdmLink wdmLink = (IBasicNetWork.WdmLink) val2;
                    for(IBasicNetWork.BasicLink link: wdmLink.takeLayerRouteLinkList()){

                        if(check(val1, link)){
                            return true;
                        }
                    }
                }else if(val2 instanceof IBasicNetWork.OtnLink){
                    IBasicNetWork.OtnLink otnLink = (IBasicNetWork.OtnLink) val2;
                    for(IBasicNetWork.BasicLink link: otnLink.takeLayerRouteLinkList()){
                        if(check(val1, link)){
                            return true;
                        }
                    }
                }else if(val2 instanceof IBasicNetWork.SdhLink){
                    IBasicNetWork.SdhLink sdhLink = (IBasicNetWork.SdhLink) val2;
                    for(IBasicNetWork.BasicLink link: sdhLink.takeLayerRouteLinkList()){
                        if(check(val1, link)){
                            return true;
                        }
                    }
                }else{
                    return false;
                }
            }

        }else{
            if(val1 instanceof IBasicNetWork.WdmLink){
                IBasicNetWork.WdmLink wdmLink = (IBasicNetWork.WdmLink) val1;
                for(IBasicNetWork.BasicLink link: wdmLink.takeLayerRouteLinkList()){
                    if(check(link, val2)){
                        return true;
                    }
                }
            }else if(val1 instanceof IBasicNetWork.OtnLink){
                IBasicNetWork.OtnLink otnLink = (IBasicNetWork.OtnLink) val1;
                for(IBasicNetWork.BasicLink link: otnLink.takeLayerRouteLinkList()){
                    if(check(link, val2)){
                        return true;
                    }
                }
            }else if(val1 instanceof IBasicNetWork.SdhLink){
                IBasicNetWork.SdhLink sdhLink = (IBasicNetWork.SdhLink) val1;
                for(IBasicNetWork.BasicLink link: sdhLink.takeLayerRouteLinkList()){
                    if(check(link, val2)){
                        return true;
                    }
                }
            }else{
                return false;
            }
        }
        return false;
    }

    public static boolean check(IBasicNetWork.BasicLink link, IBasicNetWork.BasicNode node){
        if(link == null || node == null){
            return false;
        }
        if(link.getSourceNode().equals(node) || link.getTargetNode().equals(node)){
            return true;
        }
        if(link instanceof IBasicNetWork.FiberLink){
            return false;
        }else if(link instanceof IBasicNetWork.WdmLink){
            for(IBasicNetWork.BasicLink link1: ((IBasicNetWork.WdmLink)link).takeLayerRouteLinkList()){
                if(check(link1, node)){
                    return true;
                }
            }
        }else if(link instanceof IBasicNetWork.OtnLink){
            for(IBasicNetWork.BasicLink link1: ((IBasicNetWork.OtnLink)link).takeLayerRouteLinkList()){
                if(check(link1, node)){
                    return true;
                }
            }
        }else if(link instanceof IBasicNetWork.SdhLink){
            for(IBasicNetWork.BasicLink link1: ((IBasicNetWork.SdhLink)link).takeLayerRouteLinkList()){
                if(check(link1, node)){
                    return true;
                }
            }
        }else {
            return false;
        }
        return false;
    }

}
